{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "83d3658f-6bb8-4a62-98e2-8d786fba3775",
   "metadata": {},
   "source": [
    "## 数据排序\n",
    "- 由于"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "493a1385-4498-4337-98ab-86b7aff8a8f8",
   "metadata": {},
   "source": [
    "### 按索引排序\n",
    "- sort_index() 可以用于行索引或列索引进行排序\n",
    "- sort_index(axis = 0, level=None, ascending=True, inplace=False,\n",
    "              kind='quicksort', na_position='last', sort_remaining=True)\n",
    "- axis:轴索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "e59339fd-1f96-4068-836e-87b98094d86b",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5    10\n",
      "3    11\n",
      "1    12\n",
      "3    13\n",
      "2    14\n",
      "dtype: int64\n",
      "1    12\n",
      "2    14\n",
      "3    11\n",
      "3    13\n",
      "5    10\n",
      "dtype: int64\n",
      "5    10\n",
      "3    11\n",
      "3    13\n",
      "2    14\n",
      "1    12\n",
      "dtype: int64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "ser_obj = pd.Series(range(10, 15), index=[5, 3, 1, 3, 2])\n",
    "print(ser_obj)\n",
    "\n",
    "ser_obj_sort = ser_obj.sort_index()    #按索引进行升序排序\n",
    "print(ser_obj_sort)\n",
    "\n",
    "ser_obj_sort2 = ser_obj.sort_index(ascending=False)    ##按索引进行降序排序\n",
    "print(ser_obj_sort2)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7103438f-306b-4f33-9d2c-a02114a704e6",
   "metadata": {},
   "source": [
    "对DataFrame的索引进行排序"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "4fd3e4e5-a0dd-4b61-b458-2b87bde2a9cf",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "   0  1  2\n",
      "4  0  1  2\n",
      "3  3  4  5\n",
      "5  6  7  8\n",
      "   0  1  2\n",
      "3  3  4  5\n",
      "4  0  1  2\n",
      "5  6  7  8\n",
      "   0  1  2\n",
      "5  6  7  8\n",
      "4  0  1  2\n",
      "3  3  4  5\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "df_obj = pd.DataFrame(np.arange(9).reshape(3,3), index=[4,3,5])\n",
    "print(df_obj)\n",
    "\n",
    "print(df_obj.sort_index())    #按索引的升序排序\n",
    "\n",
    "print(df_obj.sort_index(ascending=False))    #按索引的降序排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7d1a0a0b-7d22-420c-8d19-16e662b36792",
   "metadata": {},
   "source": [
    "### 按值排序\n",
    "- sort_values()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6a7cca8b-b324-41fd-97d9-e3e4ec5e0dc5",
   "metadata": {},
   "outputs": [],
   "source": [
    "- sort_values(buuy, axis=0, ascending)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "13fb01c0-85c3-483d-8c29-3580fa6d0b81",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0    4.0\n",
      "1    NaN\n",
      "2    6.0\n",
      "3    NaN\n",
      "4   -3.0\n",
      "5    2.0\n",
      "dtype: float64\n",
      "4   -3.0\n",
      "5    2.0\n",
      "0    4.0\n",
      "2    6.0\n",
      "1    NaN\n",
      "3    NaN\n",
      "dtype: float64\n",
      "2    6.0\n",
      "0    4.0\n",
      "5    2.0\n",
      "4   -3.0\n",
      "1    NaN\n",
      "3    NaN\n",
      "dtype: float64\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "ser_obj = pd.Series([4, np.nan, 6, np.nan, -3, 2])\n",
    "print(ser_obj)\n",
    "\n",
    "print(ser_obj.sort_values())   #按值升序排序\n",
    "\n",
    "print(ser_obj.sort_values(ascending=False))   #按值降序排序"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba2b121d-7565-4430-943e-2afe2b648261",
   "metadata": {},
   "source": [
    "- 需要注意的是：当Series对象调用sort_values()进行排序时，所有缺失值都会默认放到末尾\n",
    "- 在DataFrame中，sort_values()可以根据一个或多个列中的值进行排序，但需要在排序时将一个或多个列表的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8269eb91-a28c-4ebd-871c-0c57c23fcf06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "     0    1    2    3\n",
      "0  0.4 -0.1 -0.3  0.0\n",
      "1  0.2  0.6 -0.1 -0.7\n",
      "2  0.8  0.6 -0.5  0.1\n",
      "     0    1    2    3\n",
      "2  0.8  0.6 -0.5  0.1\n",
      "0  0.4 -0.1 -0.3  0.0\n",
      "1  0.2  0.6 -0.1 -0.7\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "df_obj = pd.DataFrame([[0.4, -0.1, -0.3, 0.0],\n",
    "                     [0.2, 0.6, -0.1, -0.7],\n",
    "                     [0.8, 0.6, -0.5, 0.1]])\n",
    "print(df_obj)\n",
    "print(df_obj.sort_values(by=2))     #对列索引为2的数据进行操作"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6d3ab117-5f06-4a8c-8b0a-5b718402f1d5",
   "metadata": {},
   "source": [
    "由于以上程序是按值排序，且by=2意味着索引为2的数值"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
